package cn.ylz.ylmall.content.dao.persist.repository.impl;

import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.common.util.PageInfoToPageDataConverter;
import cn.ylz.ylmall.content.dao.persist.mapper.ContentMapper;
import cn.ylz.ylmall.content.dao.persist.repository.IContentRepository;
import cn.ylz.ylmall.content.pojo.entity.Content;
import cn.ylz.ylmall.content.pojo.param.ContentAddNewParam;
import cn.ylz.ylmall.content.pojo.vo.ContentListItemVO;
import cn.ylz.ylmall.content.pojo.vo.ContentStandardVO;
import cn.ylz.ylmall.content.pojo.vo.CountVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ContentRepositoryImpl implements IContentRepository {

    @Autowired
    private ContentMapper contentMapper;

    @Override
    public int insert(Content content) {
        log.debug("开始执行[插入内容]...");
        return contentMapper.insert(content);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行[根据ID删除内容]...");
        return contentMapper.deleteById(id);
    }

    @Override
    public ContentStandardVO getStandardById(Long id) {
        log.debug("开始执行[根据ID查询内容]...");
        return contentMapper.getStandardById(id);
    }

    @Override
    public PageData<ContentListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询文章列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<ContentListItemVO> list = contentMapper.list();
        PageInfo<ContentListItemVO> pageInfo = new PageInfo<>(list);
        PageData<ContentListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<ContentListItemVO> listByCategoryId(Long categoryId, Integer pageNum, Integer pageSize) {
        log.debug("开始执行【根据分类ID查询文章列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<ContentListItemVO> list = contentMapper.listByCategoryId(categoryId);
        PageInfo<ContentListItemVO> pageInfo = new PageInfo<>(list);
        PageData<ContentListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public int updateViewCount(Long id) {
      return contentMapper.updateViewCount(id);
    }

    @Override
    public void update(Long id,Long likeCount,Long commentCount,Long collectCount) {
        contentMapper.updateCount(id,likeCount,commentCount,collectCount);
    }

    @Override
    public int updateContent(Long id,ContentAddNewParam contentAddNewParam) {
        System.out.println("id = " + id + ", contentAddNewParam = " + contentAddNewParam);
        return contentMapper.updateContent(id,contentAddNewParam);
    }


}
